# -*- coding: utf-8 -*-
# loadargs 将文本文件中各行的参数转换为list，分为：名称、值、注释
#dumpargs，将list中的内容导出到文本文件
#dtupdateargs，将list中的内容更新
#getargs，获取某个参数
#setargsvalue，将内存中的变量值更新
import sys

def loadargs(conffile):
    conff=open(conffile,'rb')
    argslist=[]
    while 1:
        if sys.version_info[0] == 2 :  #python 2, str
            argstr=conff.readline()
        else:  # python 3, bytes -> str
            argstr=bytes.decode(conff.readline())
    ##    if ( ustr and ustr[0]!='#' and ustr[0]!='\n'):  # #注释
        argslist.append(argstr)
        if not argstr:break
    argsname=[]
    argsvalue=[]
    argscomm=[]
    argsdict={}
    k=0
    ##pdb.set_trace()
    for i in argslist:
        tempstr=i.strip()
    ##    print tempstr
        if  not tempstr or tempstr[0]=='#' or tempstr[0]=='\n':
            argsname.append('')
            argsvalue.append('')
            argscomm.append(tempstr)
        else:
            index=tempstr.find('#')
            if index >=0:
                argscomm.append(tempstr[index:])
                dd=tempstr[:index].split('=')
                argsname.append(dd[0])
                if(len(dd)>1):
                    argsvalue.append(dd[1])
                else:
                    argsvalue.append('')
                argsdict[dd[0]]=k
            else:
                argscomm.append('')
                dd=tempstr.split('=')
                argsname.append(dd[0])
##                pdb.set_trace()
                if(len(dd)>1):
                    argsvalue.append(dd[1])
                else:
                    argsvalue.append('')
                argsdict[dd[0]]=k
        k=k+1
    if argsname[0][0:3]=='\xef\xbb\xbf':
        argsname[0]=argsname[0][4:]
    conff.close()
    return argscomm,argsname,argsvalue,argsdict

def dumpargs(argscomm,argsname,argsvalue,conffile):
    conff=open(conffile,'wb')
    for i in range(len(argscomm)):
        if sys.version_info[0] == 2 :  #python 2 , str
            if argsname[i]:
                conff.write(argsname[i]+'='+argsvalue[i]+argscomm[i]+'\r\n')
            else:
                conff.write(argsname[i]+argsvalue[i]+argscomm[i]+'\r\n')
        else:  #python 3, str -> bytes
            if argsname[i]:
                conff.write(str.encode(argsname[i]+'='+argsvalue[i]+argscomm[i]+'\r\n'))
            else:
                conff.write(str.encode(argsname[i]+argsvalue[i]+argscomm[i]+'\r\n'))
    conff.close()

def dtupdateargs(argsvalue,argsdict,keystr,valuestr,comment):

##    keybyte=bytes(keystr,'utf8')
    if keystr in argsdict.keys():
        k=argsdict[keystr]
##        argsvalue[k]=bytes(valuestr,'utf8')
        argsvalue[k]=valuestr
        if comment:
            argscomm[k]=argscomm[k]+'  #'+comment


def getargs(argsvalue,argsdict,keystr):
##    keybyte=bytes(keystr,'utf8')
    if keystr in argsdict.keys():
        k=argsdict[keystr]
        return argsvalue[k]
    return ''

def setargsvalue(argsvalue,bianliang):
##    tstr=str(type(vars()[keystr]))
##    if keystr in vars().keys():
##        if 'str' in tstr:
##            vars()[keystr]=keystr
##        elif 'int' in tstr:
##            vars()[keystr]=int(keystr)
##        elif 'float' in tstr:
##            vars()[keystr]=float(keystr)
##        elif 'list' in tstr:  #[]
##            vars()[keystr]=keystr.split(',')
##        elif 'dict' in tstr:  #{}
##            templist=keystr.split(',')
##            for i in templist:
##                a=i.split(':')
##                vars()[keystr][a[0]]=a[1]
##        return bianliang
##    return 'nobianliang'
    tstr=str(type(bianliang))
    if 'str' in tstr:
        bianliang=argsvalue
    elif 'int' in tstr:
        bianliang=int(argsvalue)
    elif 'float' in tstr:
        bianliang=float(argsvalue)
    elif 'list' in tstr:  #[]
        bianliang=argsvalue.split(',')
    elif 'dict' in tstr:  #{}
        templist=argsvalue.split(',')
        for i in templist:
            a=i.split(':')
            bianliang[a[0]]=a[1]
    return bianliang


##def deleteargs():


##test
test=0

if test:
    import pdb
    serverip='localhost'
    conffile='./conf.txt'
    pdb.set_trace()
    argscomm,argsname,argsvalue,argsdict=loadargs(conffile)
##    dumpargs(argscomm,argsname,argsvalue,conffile)

    ##urlset='/dtconfig/dtupdateargs/serverip:192.168.0.200# test ip'
    ##
    ##urlget='/dtconfig/getargs/serverip'
    keystr='serverip'
    serveripargs=getargs(argsvalue,argsdict,keystr)
    print(serveripargs)
    dtupdateargs(argsvalue,argsdict,keystr,'192.168.0.120',' #test ip')
    serverip=setargsvalue('192.168.0.120','')
    
    serveripargs=getargs(argsvalue,argsdict,keystr)
    print(serveripargs,'\r\n',serverip)
    conffile='./temp.txt'
    dumpargs(argscomm,argsname,argsvalue,conffile)

